package com.collect.senegal.zhiScene.zhiCollection.zhiGroup.zhiPerformance.zhiDaily;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.annotation.SuppressLint;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.collect.senegal.R;
import com.collect.senegal.zhiAdater.zhiLevelDetail.ZHILevelDetailGroupDaily;
import com.collect.senegal.zhiBase.BaseActivity;
import com.collect.senegal.zhiBean.zhiReceive.ZHIRPerfermanceGroupDailyEntity;
import com.collect.senegal.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.senegal.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.senegal.zhiScene.zhiCollection.zhiGroup.zhiPerformance.zhiSort.ZHIGroupDailyCompareByTotalNumASC;
import com.collect.senegal.zhiScene.zhiWidget.zhiNavigationBar.ZHINavigationBar;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
@Route(path = ZHIProjectRouter.ZHI_PERFORMANCE_GROUP_DAILY)
public class ZHIPerformanceGroupDailyActivity extends BaseActivity<ZHIPerformanceGroupDailyPresenter> implements ZHIPerformanceGroupDailyContract.View {

    @BindView(R.id.navigationBar)
    ZHINavigationBar navigationBar;

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    private final ZHILevelDetailGroupDaily mZHILevelDetailGroupDaily = new ZHILevelDetailGroupDaily();

    private ArrayList<ZHIRPerfermanceGroupDailyEntity> mZhirPerfermanceGroupRealTimeEntityArrayList;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_performance_group_daily;
    }

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void initView() {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            navigationBar.hideRight().setCenterText(getString(R.string.page_daily)).setBarClickListener(new ZHINavigationBar.OnNavigationBarClickListener() {
                @Override
                public void onNavigationBarLeftClick(View v) {
                    finish();
                }

                @Override
                public void onNavigationBarRightClick(View v) {
                }
            });


            mZHILevelDetailGroupDaily.setEmptyView(getRefreshEmptyView());

            rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
            rc_detail_list.setAdapter(mZHILevelDetailGroupDaily);

            if (mPresenter != null) {
                mPresenter.getGroupDailyList(ZHIPerformanceGroupDailyActivity.this, new ZHISPerfermanceEntity(), true);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onGetGroupDailyList(ArrayList<ZHIRPerfermanceGroupDailyEntity> zhirPerfermanceGroupDailyEntityArrayList, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            if (mZhirPerfermanceGroupRealTimeEntityArrayList == null) {
                mZhirPerfermanceGroupRealTimeEntityArrayList = new ArrayList<>();
            }
            mZhirPerfermanceGroupRealTimeEntityArrayList.clear();

            mZhirPerfermanceGroupRealTimeEntityArrayList.addAll(zhirPerfermanceGroupDailyEntityArrayList);

            Collections.sort(mZhirPerfermanceGroupRealTimeEntityArrayList, new ZHIGroupDailyCompareByTotalNumASC());

            mZHILevelDetailGroupDaily.setNewData(mZhirPerfermanceGroupRealTimeEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void initData(boolean showDialog) {
        if (mPresenter != null) {
            mPresenter.getGroupDailyList(ZHIPerformanceGroupDailyActivity.this, new ZHISPerfermanceEntity(), showDialog);
        }
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }
}